package com.block.api.config;


import com.block.api.interceptor.InitInterceptor;
import com.block.api.interceptor.InitSourceInterceptor;
import com.block.api.interceptor.InitUnionInterceptor;
import com.block.common.interceptor.RequestLimitInterceptor;
import com.block.common.interceptor.SignInterceptor;
import com.block.common.interceptor.TokenInterceptor;
import com.block.test.interceptor.Param2ValidateInterceptor;
import com.block.test.interceptor.ParamValidateInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * 拦截器配置类
 * @author JackieQ
 * @since 2019/3/1
 */

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    public static List<String> excludePathList = new ArrayList<>();
    static {
        excludePathList.add("/v1/**");
        excludePathList.add("/exp/**");
        excludePathList.add("/ext/**");
        excludePathList.add("/apiflow/**");
        excludePathList.add("/apiunion/**");
        excludePathList.add("/page/**");
        excludePathList.add("/apinotice/**");
        excludePathList.add("/apisource/**");
    }

    @Bean
    InitSourceInterceptor initSourceInterceptor(){
        return new InitSourceInterceptor();
    }
    @Bean
    InitUnionInterceptor initUnionInterceptor(){
        return new InitUnionInterceptor();
    }
    @Bean
    InitInterceptor initInterceptor(){
        return new InitInterceptor();
    }
    @Bean
    SignInterceptor signInterceptor(){
        return new SignInterceptor();
    }
    @Bean
    TokenInterceptor tokenInterceptor(){
        return new TokenInterceptor();
    }
    @Bean
    RequestLimitInterceptor requestLimitInterceptor(){
        return  new RequestLimitInterceptor();
    }

    /**
     * 数据合法性校验拦截器
     * @return
     */
    @Bean
    ParamValidateInterceptor paramValidateInterceptor(){
        return  new ParamValidateInterceptor();
    }

       @Bean
       Param2ValidateInterceptor param2ValidateInterceptor(){
        return  new Param2ValidateInterceptor();
    }

    /**
     * 添加拦截器
     *
     * @param registry 注册拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //参数处理
        registry.addInterceptor(initInterceptor())
                .addPathPatterns("/api/**")
                .excludePathPatterns(excludePathList);
        //请求签名
        registry.addInterceptor(signInterceptor())
                .addPathPatterns("/api/**")
                .excludePathPatterns(excludePathList);

        registry.addInterceptor(requestLimitInterceptor())
                .addPathPatterns("/api/**")
                .addPathPatterns("/**")
                .excludePathPatterns(excludePathList);
        //除去登录
        registry.addInterceptor(tokenInterceptor())
                .addPathPatterns("/api/**")
                .excludePathPatterns("/api/member/login")
                .excludePathPatterns(excludePathList);

        //数据合法性校验拦截器
        registry.addInterceptor(paramValidateInterceptor())
                .addPathPatterns("/api/**")
                .excludePathPatterns(excludePathList);

        //联登拦截器
        registry.addInterceptor(initUnionInterceptor())
                .addPathPatterns("/apiunion/**");

        //线索拦截器
        registry.addInterceptor(initSourceInterceptor())
                .addPathPatterns("/apisource/**");
        registry.addInterceptor(param2ValidateInterceptor())
                .addPathPatterns("/apisource/**");

    }

}
